/**
 * 
 */
package com.gxt.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author JasonGuo
 * @Description:
 */
public class MergeVideoMp3 {
	private String ffmpegEXE;
	
	public MergeVideoMp3(String ffmpegEXE) {
		super();
		this.ffmpegEXE = ffmpegEXE;
	}
	
	public void convertor(String videoInputPath, String mp3InputPath,
			double seconds, String videoOutputPath) throws IOException {
//		$ ffmpeg -i input.mp4 -i bgm.mp3 -t 7 -y 新的视频.mp4
		
//		先初始化随机数 作为中间中转文件的文件名区分
		String ran = UUID.randomUUID().toString();
		
		
//		第一步
		List<String> command1 = new ArrayList<>();
		command1.add(ffmpegEXE);
		command1.add("-i");
		command1.add(videoInputPath);
		
		command1.add("-vcodec");
		command1.add("copy");
		
		command1.add("-an");
		command1.add("E:\\gxt_videos\\"+ ran+".mp4");

		for(String a : command1) {
			System.out.println(a);
		}
		
		ProcessBuilder builder1 = new ProcessBuilder(command1);
		Process process1 = builder1.start();
		
//		由于做处理时会产生临时文件 占用线程
//		为防止资源浪费进行处理
		
		InputStream errorStream1 = process1.getErrorStream();
		InputStreamReader inputStreamReader1 = new InputStreamReader(errorStream1);
		BufferedReader br1 = new BufferedReader(inputStreamReader1);
//		对br作处理 解决上述问题
		
		String line = ""; //每一行读取出来
		while((line = br1.readLine()) != null) {
//			读到null才停
		}
		if(br1 != null) {
			br1.close();
		}
		
		if(inputStreamReader1 != null) {
			inputStreamReader1.close();
		}
		
		if(errorStream1 != null) {
			errorStream1.close();
		}
		
//		第二步
		List<String> command = new ArrayList<>();
		command.add(ffmpegEXE);
		command.add("-i");
		command.add("E:\\gxt_videos\\"+ ran + ".mp4");
		
		command.add("-i");
		command.add(mp3InputPath);
		
		command.add("-t");
		command.add(String.valueOf(seconds));
		
		command.add("-y");
		command.add(videoOutputPath);
		
		System.out.println("-------------------------*------------------------");
		
		for(String a : command) {
			System.out.println(a);
		}
		
		ProcessBuilder builder = new ProcessBuilder(command);
		Process process = builder.start();
		
//		由于做处理时会产生临时文件 占用线程
//		为防止资源浪费进行处理
		
		InputStream errorStream = process.getErrorStream();
		InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
		BufferedReader br = new BufferedReader(inputStreamReader);
//		对br作处理 解决上述问题
		
		String line2 = ""; //每一行读取出来
		while((line2 = br.readLine()) != null) {
//			读到null才停
		}
		if(br != null) {
			br.close();
		}
		
		if(inputStreamReader != null) {
			inputStreamReader.close();
		}
		
		if(errorStream != null) {
			errorStream.close();
		}
		
	}
//	public static void main(String[] args) {
//		MergeVideoMp3 m = new MergeVideoMp3("E:\\ffmpeg\\bin\\ffmpeg.exe");
//		try {
////			E:\\gxt_videos\\180425CFA4RB6T0H\\video\\test.mp4
//			m.convertor("F:\\BaiduNetdiskDownload\\TED\\Is life really that complex_HannahFry_2012X-480p-zh-cn.mp4", 
//					"E:\\gxt_videos\\bgm\\林俊杰-江南.mp3",120,"E:\\gxt_videos\\180425CFA4RB6T0H\\video\\冲冲冲2.mp4");
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
//	}

}
